/*
 * Created by JFormDesigner on Wed May 07 08:50:47 CST 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.Account;
import com.soft.bams.exception.NotFoundException;
import com.soft.bams.exception.RegisterException;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

/**
 * 注册面板
 */
public class RegisterPanel extends JPanel {
    private ATMClient atm;  // 依赖的ATM客户端实例

    // 构造方法：接收ATMClient实例
    public RegisterPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();  // 初始化界面组件
    }

    // 事件：点击取消按钮，返回主界面
    private void Back(ActionEvent e) {
        // 先清空表单
        clearForm();
        // 然后切换到主面板
        CardLayout cardLayout = (CardLayout) getParent().getLayout();
        cardLayout.show(getParent(), "mainPanel");
    }

    // 事件：点击注册按钮，提交注册信息
    private void register(ActionEvent e) {
        // 从输入框获取用户输入
        String userName = nameField.getText().trim();          // 用户名
        String personId = personIdField.getText().trim();      // 身份证号
        String email = emailField.getText().trim();            // 邮箱
        String password = new String(passwordField.getPassword());  // 密码（JPasswordField安全获取）
        String confirmPassword = new String(repasswordField.getPassword());  // 确认密码
        int accountType = comboBox.getSelectedIndex();         // 账户类型（下拉框索引）

        /*try {
            // 调用银行服务注册账户
            Account newAccount = atm.bank.register(
                    password, confirmPassword, userName, personId, email, accountType
            );

            if (newAccount != null) {
                // 注册成功：更新ATM客户端的当前账户，切换到业务面板
                atm.acc = newAccount;
                atm.businessPanel.initBusiness();  // 初始化业务面板
                CardLayout cardLayout = (CardLayout) getParent().getLayout();
                cardLayout.show(getParent(), "businessPanel");  // 切换到业务面板
                clearForm();  // 清空注册表单
            }
        } catch (NotFoundException | RegisterException ex) {
            // 注册失败：弹窗提示错误信息，并清空密码框
            JOptionPane.showMessageDialog(this, ex.getMessage(), "注册失败", JOptionPane.ERROR_MESSAGE);
            passwordField.setText("");         // 清空密码框
            repasswordField.setText("");       // 清空确认密码框
        }
*/
        atm.pw.println("register#" + password + "#" + confirmPassword + "#" +
                userName + "#" + personId + "#" + email + "#" + accountType);
        atm.pw.flush();
        Account register = null;
        try {
            Object o = atm.ois.readObject();
            if(o instanceof Account){
                register = (Account) o;
            }else if(o instanceof String){
                JOptionPane.showMessageDialog(this , o);
            }
            if (register != null){
                JOptionPane.showMessageDialog(this, "注册成功！");
                //初始化BusinessPanel
                atm.acc = register;
                atm.businessPanel.initBusiness();

                //跳转到BusinessPanel
                CardLayout layout = (CardLayout) this.getParent().getLayout();
                layout.show(this.getParent(), "businessPanel");

                clearForm();
            }
        } catch (IOException | ClassNotFoundException ex) {
            //开户失败
            // 1.弹窗提醒失败信息
            JOptionPane.showMessageDialog(null, ex.getMessage());
            // 2.清空密码相关的输入框
            passwordField.setText("");
            repasswordField.setText("");
        }


    }

    // 事件：点击重置按钮，清空表单
    private void reSet(ActionEvent e) {
        clearForm();
    }

    // 功能：清空所有输入框
    private void clearForm() {
        nameField.setText("");         // 用户名
        personIdField.setText("");     // 身份证号
        addrField.setText("");         // 地址
        emailField.setText("");        // 邮箱
        passwordField.setText("");     // 密码
        repasswordField.setText("");   // 确认密码
        comboBox.setSelectedIndex(0);  // 下拉框重置为第一个选项（储蓄账户）
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        titlelabel = new JLabel();
        backbutton = new JButton();
        typelable = new JLabel();
        pwdLable = new JLabel();
        repwdLable = new JLabel();
        personIdlable = new JLabel();
        addrlable = new JLabel();
        emaillable = new JLabel();
        comboBox = new JComboBox<>();
        passwordField = new JPasswordField();
        repasswordField = new JPasswordField();
        personIdField = new JTextField();
        addrField = new JTextField();
        emailField = new JTextField();
        registerbutton = new JButton();
        reSetbutton = new JButton();
        namelabel = new JLabel();
        nameField = new JTextField();

        //======== this ========
        setLayout(null);

        //---- titlelabel ----
        titlelabel.setText("\u6ce8 \u518c \u754c \u9762");
        titlelabel.setHorizontalAlignment(SwingConstants.CENTER);
        titlelabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        add(titlelabel);
        titlelabel.setBounds(new Rectangle(new Point(205, 45), titlelabel.getPreferredSize()));

        //---- backbutton ----
        backbutton.setText("\u53d6 \u6d88");
        backbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        backbutton.addActionListener(e -> Back(e));
        add(backbutton);
        backbutton.setBounds(320, 435, 78, 34);

        //---- typelable ----
        typelable.setText("\u8d26 \u6237 \u7c7b \u578b:");
        typelable.setHorizontalAlignment(SwingConstants.CENTER);
        typelable.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(typelable);
        typelable.setBounds(45, 105, 108, 30);

        //---- pwdLable ----
        pwdLable.setText("\u5bc6  \u7801:");
        pwdLable.setHorizontalAlignment(SwingConstants.CENTER);
        pwdLable.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(pwdLable);
        pwdLable.setBounds(85, 190, 65, 30);

        //---- repwdLable ----
        repwdLable.setText("\u786e \u5b9a \u5bc6 \u7801:");
        repwdLable.setHorizontalAlignment(SwingConstants.CENTER);
        repwdLable.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(repwdLable);
        repwdLable.setBounds(50, 235, 103, 30);

        //---- personIdlable ----
        personIdlable.setText("\u8eab \u4efd \u8bc1 \u53f7:");
        personIdlable.setHorizontalAlignment(SwingConstants.CENTER);
        personIdlable.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(personIdlable);
        personIdlable.setBounds(50, 285, 103, 30);

        //---- addrlable ----
        addrlable.setText("\u8be6 \u7ec6 \u5730 \u5740:");
        addrlable.setHorizontalAlignment(SwingConstants.CENTER);
        addrlable.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(addrlable);
        addrlable.setBounds(45, 325, 113, 30);

        //---- emaillable ----
        emaillable.setText("E-mail:");
        emaillable.setHorizontalAlignment(SwingConstants.CENTER);
        emaillable.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(emaillable);
        emaillable.setBounds(85, 365, 65, 30);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u50a8\u84c4\u8d26\u6237",
                "\u4fe1\u7528\u8d26\u6237",
                "\u53ef\u8d37\u6b3e\u50a8\u84c4\u8d26\u6237",
                "\u53ef\u8d37\u6b3e\u4fe1\u7528\u8d26\u6237"
        }));
        comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(comboBox);
        comboBox.setBounds(221, 105, 155, 34);

        //---- passwordField ----
        passwordField.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
        add(passwordField);
        passwordField.setBounds(221, 190, 155, 34);

        //---- repasswordField ----
        repasswordField.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
        add(repasswordField);
        repasswordField.setBounds(221, 235, 155, 34);

        //---- personIdField ----
        personIdField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(personIdField);
        personIdField.setBounds(221, 280, 155, 34);

        //---- addrField ----
        addrField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(addrField);
        addrField.setBounds(221, 320, 155, 34);

        //---- emailField ----
        emailField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(emailField);
        emailField.setBounds(221, 360, 155, 34);

        //---- registerbutton ----
        registerbutton.setText("\u6ce8 \u518c");
        registerbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        registerbutton.addActionListener(e -> register(e));
        add(registerbutton);
        registerbutton.setBounds(60, 435, 78, 34);

        //---- reSetbutton ----
        reSetbutton.setText("\u91cd \u7f6e");
        reSetbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        reSetbutton.addActionListener(e -> reSet(e));
        add(reSetbutton);
        reSetbutton.setBounds(190, 435, 78, 34);

        //---- namelabel ----
        namelabel.setText("\u7528 \u6237 \u540d:");
        namelabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(namelabel);
        namelabel.setBounds(new Rectangle(new Point(80, 155), namelabel.getPreferredSize()));

        //---- nameField ----
        nameField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(nameField);
        nameField.setBounds(221, 150, 155, 34);

        setPreferredSize(new Dimension(500, 600));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel titlelabel;
    private JButton backbutton;
    private JLabel typelable;
    private JLabel pwdLable;
    private JLabel repwdLable;
    private JLabel personIdlable;
    private JLabel addrlable;
    private JLabel emaillable;
    private JComboBox<String> comboBox;
    private JPasswordField passwordField;
    private JPasswordField repasswordField;
    private JTextField personIdField;
    private JTextField addrField;
    private JTextField emailField;
    private JButton registerbutton;
    private JButton reSetbutton;
    private JLabel namelabel;
    private JTextField nameField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}

